package com.vilicode.controller;

import com.vilicode.bean.Page;
import com.vilicode.bean.PostType;
import com.vilicode.service.PostService;
import com.vilicode.service.PostTypeService;
import org.springframework.stereotype.Controller;
import com.vilicode.bean.Post;

import com.vilicode.bean.User;
import com.vilicode.mapper.RecommendMapper;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

@Controller
public class PostController {
    @Autowired
    public PostService postService;
    @Autowired
    public PostTypeService postTypeService;

    @RequestMapping("/posttypes_list")//pageNumber=1&ptid=-1
    public String showPostsByPostTypeID(int pageNumber, int ptid,HttpServletRequest request)
    {
        String ptname="";
        if(ptid!=0)
        {
            ptname=postTypeService.queryPostTypeNameByPostTypeID(ptid);
        }
        request.setAttribute("t",ptname);
        //List<Goods> list=gService.selectGoodsByTypeID(id,1,8);
        //request.setAttribute("goodsList",list);
        if(pageNumber<=0)
            pageNumber=1;
        Page p=postTypeService.queryPostByPostTypeID(ptid,pageNumber);

        if(p.getTotalPage()==0)
        {
            p.setTotalPage(1);
            p.setPageNumber(1);
        }
        else {
            if(pageNumber>=p.getTotalPage()+1)
            {
                p=postTypeService.queryPostByPostTypeID(ptid,p.getTotalPage());
            }
        }

        request.setAttribute("p",p);
        request.setAttribute("ptid",ptid);
        return "booktypes_list";
    }
    @RequestMapping("/post_detail")
    public String showPostByID(int pid,HttpServletRequest request)
    {
        Post post= postService.queryPostByID(pid);
        request.setAttribute("post",post);
        return "book_detail";
    }








}
